package cn.amebius.testmethod;

import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.ExecCreateCmdResponse;
import com.github.dockerjava.api.command.ExecStartCmd;
import com.github.dockerjava.api.command.LogContainerCmd;
import com.github.dockerjava.api.model.Frame;
import com.github.dockerjava.core.command.ExecStartResultCallback;
import com.github.dockerjava.core.command.LogContainerResultCallback;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.concurrent.TimeUnit;

/**
 * @author 阿伟
 * @date 2023/7/21 11:59
 */
public class ExecTestMethod {

    public void execInConsole(DockerClient dockerClient, String containerId) throws InterruptedException {
        LogContainerCmd logContainerCmd = dockerClient.logContainerCmd(containerId)
                                                        .withStdOut(true)
                                                        .withStdErr(true)
                                                        .withFollowStream(true);
                                                        //.withTail(2);
//        dockerClient.attachContainerCmd()
        LogContainerResultCallback logContainerResultCallback = logContainerCmd.exec(new LogContainerResultCallback() {
            @Override
            public void onNext(Frame item) {
                System.out.println("【日志信息】" + new String(item.getPayload()));
//                StreamType streamType = item.getStreamType();

            }
            @Override
            public LogContainerResultCallback awaitCompletion() throws InterruptedException {
                System.out.println("【等待完成...】");
                return super.awaitCompletion();
            }
            @Override
            public LogContainerResultCallback awaitStarted() throws InterruptedException {
                System.out.println("【等待开始...】");
                return super.awaitStarted();
            }
            @Override
            public void onComplete() {
                System.out.println("【完成】");
                super.onComplete();
            }
        }).awaitCompletion();;
    }

    public void execInputAndOutput(DockerClient dockerClient, String containerId) throws InterruptedException {
//        ExecCreateCmdResponse execCreateCmdResponse = dockerClient.execCreateCmd(containerId)
//                .withCmd("bash")
//                .withAttachStdin(true)
//                .withAttachStdout(true)
//                .exec();
//        String execId = execCreateCmdResponse.getId();
//
//        ExecStartCmd execStartCmd = dockerClient.execStartCmd(execId)
//                .withDetach(false)
//                .withTty(true);
//
//        InputStream inputStream = System.in;
//        execStartCmd.withStdIn(inputStream).exec();


    }

}
